﻿// Decompiled with JetBrains decompiler
// Type: StoryMode.Quests.QuestTasks.PurchaseItemTutorialQuestTask
// Assembly: StoryMode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 11BAE65F-6C15-4628-A9C6-1B968588CDA1
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\StoryMode\bin\Win64_Shipping_Client\StoryMode.dll

using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.SaveSystem;

#nullable disable
namespace StoryMode.Quests.QuestTasks
{
  public class PurchaseItemTutorialQuestTask : QuestTaskBase
  {
    [SaveableField(1)]
    private readonly JournalLog _progressLog;
    private int _targetItemAmount;
    private ItemObject _item;
    [SaveableField(2)]
    private int _purchasedItemAmount;

    public PurchaseItemTutorialQuestTask(
      Action onSucceed,
      int targetItemAmount,
      ItemObject item,
      JournalLog progressLog = null)
      : base(onSucceedAction: onSucceed)
    {
      this._targetItemAmount = targetItemAmount;
      this._item = item;
      this._progressLog = progressLog;
      this._purchasedItemAmount = 0;
    }

    public void InitializeTaskOnLoad(int targetItemAmount, ItemObject item)
    {
      this._targetItemAmount = targetItemAmount;
      this._item = item;
    }

    public override void SetReferences()
    {
      CampaignEvents.PlayerInventoryExchangeEvent.AddNonSerializedListener((object) this, new Action<List<(ItemRosterElement, int)>, List<(ItemRosterElement, int)>, bool>(this.PlayerInventoryExchange));
    }

    private void PlayerInventoryExchange(
      List<(ItemRosterElement, int)> purchasedItems,
      List<(ItemRosterElement, int)> soldItems,
      bool isTrading)
    {
      if (!this.IsActive)
        return;
      foreach ((ItemRosterElement, int) purchasedItem in purchasedItems)
      {
        ItemRosterElement itemRosterElement = purchasedItem.Item1;
        if (itemRosterElement.EquipmentElement.Item == this._item)
        {
          int purchasedItemAmount = this._purchasedItemAmount;
          itemRosterElement = purchasedItem.Item1;
          int amount = itemRosterElement.Amount;
          this._purchasedItemAmount = purchasedItemAmount + amount;
          if (this._purchasedItemAmount >= this._targetItemAmount)
          {
            this._progressLog?.UpdateCurrentProgress(this._targetItemAmount);
            this.Finish(QuestTaskBase.FinishStates.Success);
            break;
          }
          this._progressLog?.UpdateCurrentProgress(this._purchasedItemAmount);
        }
      }
    }

    internal static void AutoGeneratedStaticCollectObjectsPurchaseItemTutorialQuestTask(
      object o,
      List<object> collectedObjects)
    {
      ((QuestTaskBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._progressLog);
    }

    internal static object AutoGeneratedGetMemberValue_progressLog(object o)
    {
      return (object) ((PurchaseItemTutorialQuestTask) o)._progressLog;
    }

    internal static object AutoGeneratedGetMemberValue_purchasedItemAmount(object o)
    {
      return (object) ((PurchaseItemTutorialQuestTask) o)._purchasedItemAmount;
    }
  }
}
